<?php

/**
 * 分页类
 * show(2)  1 ... 62 63 64 65 66 67 68 ... 150
 * 分页样式
 * #page{font:12px/16px arial}
 * #page span{float:left;margin:0px 3px;}
 * #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px 7px; text-decoration:none;color:#666}
 * #page a.now_page,#page a:hover{color:#fff;background:#05c}
 */

namespace application\Comm;

class Pager {

    public $first_row;        //起始行数
    public $list_rows;        //列表每页显示行数
    protected $total_pages;      //总页数
    protected $total_rows;       //总行数
    protected $now_page;         //当前页数
    protected $method = 'defalut'; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式
    protected $parameter = '';
    protected $page_name;        //分页参数的名称
    protected $ajax_func_name;
    public $plus = 3;         //分页偏移量
    protected $url;

    /**
     * 构造函数
     * @param unknown_type $data
     */
    public function __construct($data = array()) {

        $this->total_rows = $data['total_rows'];

        $this->parameter = !empty($data['parameter']) ? $data['parameter'] : '';
        $this->list_rows = !empty($data['list_rows']) && $data['list_rows'] <= 100 ? $data['list_rows'] : 15;
        $this->total_pages = ceil($this->total_rows / $this->list_rows);
        $this->page_name = !empty($data['page_name']) ? $data['page_name'] : 'p';
        $this->ajax_func_name = !empty($data['ajax_func_name']) ? $data['ajax_func_name'] : 'goto';

        $this->method = !empty($data['method']) ? $data['method'] : 'get';


        /* 当前页面 */
        if (!empty($data['now_page'])) {
            $this->now_page = intval($data['now_page']);
        } else {
            $this->now_page = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]) : 1;
        }
        $this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;


        if (!empty($this->total_pages) && $this->now_page > $this->total_pages) {
            $this->now_page = $this->total_pages;
        }
        $this->first_row = $this->list_rows * ($this->now_page - 1);
    }

    /**
     * 得到当前连接
     * @param $page
     * @param $text
     * @return string
     */
    protected function _get_link($page, $text, $class='') {
        switch ($this->method) {
            case 'ajax':
                $parameter = '';
                if ($this->parameter) {
                    $parameter = ',' . $this->parameter;
                }
                return '<a onclick="' . $this->ajax_func_name . '(\'' . $page . '\'' . $parameter . ')" href="javascript:void(0)">' . $text . '</a>' . "\n";
                break;

            case 'html':
                $url = str_replace('?', $page, $this->parameter);
                return '<a  class="'.$class.'" href="' . $url . '">' . $text . '</a>' . "\n";
                break;

            default:
                return '<a class="'.$class.'" href="' . $this->_get_url($page) . '">' . $text . '</a>' . "\n";
                break;
        }
    }


    /**
     * 得到$page的url
     * @param $page 页面
     * @return string
     */
    protected function _get_url($page) {
        $params = Request::getParams();
        $params[$this->page_name] = $page;
        unset($params['r']);
        $r = Request::getParam('r');
        return $this->url = Func::createUrl($r,$params);
    }

    /**
     * 得到第一页
     * @return string
     */
    public function first_page($name = '第一页') {
        if ($this->now_page > 1) {
            return $this->_get_link('1', $name);
        }
        return '';
    }

    /**
     * 最后一页
     * @param $name
     * @return string
     */
    public function last_page($name = '最后一页') {
        if ($this->now_page < $this->total_pages - 1) {
            return $this->_get_link($this->total_pages, $name);
        }
        return '';
    }

    /**
     * 上一页
     * @return string
     */
    public function up_page($name = '上一页') {
        if ($this->now_page != 1) {
            return $this->_get_link($this->now_page - 1, $name,'products_click');
        }
        return '';
    }

    /**
     * 下一页
     * @return string
     */
    public function down_page($name = '下一页') {
        if ($this->now_page < $this->total_pages) {
            return $this->_get_link($this->now_page + 1, $name,'products_click');
        }
        return '';
    }

    /**
     * 分页样式输出
     * @param $param
     * @return string
     */
    public function show($param = 1) {
        if ($this->total_rows < 1) {
            return '';
        }

        $className = 'show_' . $param;
        $classNames = get_class_methods($this);
        if (in_array($className, $classNames)) {
            return $this->$className();
        }
        return '';
    }

    protected function show_2() {
        if ($this->total_pages != 1) {
            $return = '';
            $return .= $this->up_page('<');
            for ($i = 1; $i <= $this->total_pages; $i++) {
                if ($i == $this->now_page) {
                    $return .= "<a class='now_page'>$i</a>\n";
                } else {
                    if ($this->now_page - $i >= 4 && $i != 1) {
                        $return .="<span class='pageMore'>...</span>\n";
                        $i = $this->now_page - 3;
                    } else {
                        if ($i >= $this->now_page + 5 && $i != $this->total_pages) {
                            $return .="<span>...</span>\n";
                            $i = $this->total_pages;
                        }
                        $return .= $this->_get_link($i, $i) . "\n";
                    }
                }
            }
            $return .= $this->down_page('>');
            return $return;
        }
    }

    protected function show_1() {
        $plus = $this->plus;
        if ($plus + $this->now_page > $this->total_pages) {
            $begin = $this->total_pages - $plus * 2;
        } else {
            $begin = $this->now_page - $plus;
        }

        $begin = ($begin >= 1) ? $begin : 1;
        $return = '';
        $return .= $this->first_page();
        $return .= $this->up_page();
        for ($i = $begin; $i <= $begin + $plus * 2; $i++) {
            if ($i > $this->total_pages) {
                break;
            }
            if ($i == $this->now_page) {
                $return .= "<a class='now_page'>$i</a>\n";
            } else {
                $return .= $this->_get_link($i, $i) . "\n";
            }
        }
        $return .= $this->down_page();
        $return .= $this->last_page();
        return $return;
    }

    protected function show_3() {
        $plus = $this->plus;
        if ($plus + $this->now_page > $this->total_pages) {
            $begin = $this->total_pages - $plus * 2;
        } else {
            $begin = $this->now_page - $plus;
        }
        $begin = ($begin >= 1) ? $begin : 1;
        $return = '<div class="pager">总计 ' . $this->total_rows . ' 个记录分为 ' . $this->total_pages . ' 页, 当前第 ' . $this->now_page . ' 页 ';
        $return .= ',每页显示 ';
        $return .= '<select onchange="' . $this->ajax_func_name . '(this.value)" class="gotoPage" name="limit">';
        for ($i = 1; $i <= 10; $i++) {
            $key = $i * 10;
            if ($key == $this->list_rows) {
                $return .= '<option selected="selected" value="' . $key . '">' . $key . '</option>';
            } else {
                $return .= '<option value="' . $this->_get_url('') . '&limit=' . $key . '">' . $key . '</option>';
            }
        }
        $return .= '</select>';
        $return .= $this->first_page() . "\n";
        $return .= $this->up_page() . "\n";
        $return .= $this->down_page() . "\n";
        $return .= $this->last_page() . "\n";
        $return .= '跳到: <select onchange="' . $this->ajax_func_name . '(this.value)" class="gotoPage" data-val="' . $this->now_page . '">';

        for ($i = $begin; $i <= $begin + 10; $i++) {
            if ($i > $this->total_pages) {
                break;
            }
            if ($i == $this->now_page) {
                $return .= '<option selected="selected" value="">' . $i . '</option>';
            } else {
                $return .= '<option value="' . $this->_get_url($i) . '">' . $i . '</option>';
            }
        }
        $return .= '</select>';
        $return .= '</div>';
        return $return;
    }

    protected function show_product1()
    {
        $plus = $this->plus;
        if ($plus + $this->now_page > $this->total_pages) {
            $begin = $this->total_pages - $plus * 2;
        } else {
            $begin = $this->now_page - $plus;
        }
        $begin = ($begin >= 1) ? $begin : 1;

        $return = '<div class="num_sel">';
        $return .= $this->up_page('<i><</i>') . "\n";
        if($begin>=2)$return .='<a href="javascript:;"><span>...</span></a>';
        for ($i = $begin; $i <= $begin + 6; $i++) {
            if ($i > $this->total_pages) {
                break;
            }
            if ($i == $this->now_page) {
                $return .= '<a href="javascript:;"><span class="act" >'.$i.'</span></a>';
            } else {
                $return .= '<a href="'.$this->_get_url($i).'"><span>'.$i.'</span></a>';
            }
        }
        if( $begin + 6 <$this->total_pages)$return .='<a href="javascript:;"><span>...</span></a>';
        $return .= $this->down_page('<i>></i>') . "\n";
        $return .= '</div>';

        return $return;
    }
    protected function show_product2()
    {
        $plus = $this->plus;
        if ($plus + $this->now_page > $this->total_pages) {
            $begin = $this->total_pages - $plus * 2;
        } else {
            $begin = $this->now_page - $plus;
        }
        $begin = ($begin >= 1) ? $begin : 1;
        $return = $this->up_page('<上一页') . "\n";
        $return .= '<div class="num fl">';
        if($begin>=2)$return .='<a href="javascript:;"><span>...</span></a>';
        for ($i = $begin; $i <= $begin + 6; $i++) {
            if ($i > $this->total_pages) {
                break;
            }
            if ($i == $this->now_page) {
                $return .= '<a href="javascript:;"><span class="act" >'.$i.'</span></a>';
            } else {
                $return .= '<a href="'.$this->_get_url($i).'"><span>'.$i.'</span></a>';
            }
        }
        if( $begin + 6 <$this->total_pages)$return .='<a href="javascript:;"><span>...</span></a>';
        $return .= '</div>';
        $return .= '</select>';
        $return .= $this->down_page('下一页>') . "\n";
        $return .= '<div class="page"><span>共<i>'.$this->total_pages.'</i>页</span>';
        $return .= '<!--
<div class="choice"><span>3</span><a class="pre act"></a><a class="next"></a></div><span class="sure ml_10">确定</span>-->
';
        $return .= '</div>';

        return $return;
    }

}
